/*
 * Invitation.java
 *
 * Created on September 18, 2007, 9:05 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package timeitj.persistence;

import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Version;
import org.apache.log4j.Logger;
import timeitj.ejb.SessionFacadeBean;
import timeitj.util.InvitationStatus;

/**
 * Entity class Invitation
 *
 * @author wallner
 */
@Entity
@NamedQueries({
    @NamedQuery(name  = "getInvitationsByInviter",
    query = "SELECT i FROM Invitation i WHERE i.inviter = :inviter"
        ),
        
        @NamedQuery(name  = "getInvitationsByProject",
    query = "SELECT i FROM Invitation i WHERE i.project = :project"
        ),
        
        @NamedQuery(name  = "getInvitationsForInvitee",
    query = "SELECT i FROM Invitation i WHERE i.invitee = :invitee"
        )
})

@Table(name="INVITATIONS")

public class Invitation implements Serializable {
    
    private static Logger logger = Logger.getLogger(Invitation.class.getName());
    
    @Id
    @Column(name="ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    @Version
    @Column(name="OPT_LOCK")
    private Long version;
    
    @ManyToOne(cascade = {CascadeType.MERGE,CascadeType.REMOVE})
    private Person inviter;
    
    @ManyToOne(cascade = {CascadeType.MERGE,CascadeType.REMOVE})
    private Person invitee;
    
    @ManyToOne(cascade = {CascadeType.MERGE,CascadeType.REMOVE})
    private Project project;
    
    @Enumerated(EnumType.ORDINAL)
    @Column(name="STATUS")
    private InvitationStatus status;
    
    /** Creates a new instance of Invitation */
    public Invitation() {
    }
    
    public Invitation(Person inviter, Person invitee, Project project){
        this.inviter = inviter;
        this.invitee = invitee;
        this.project = project;
        logger.info("'" + inviter.getLogin() + "' invites '" + invitee.getLogin() + "' to Project '" + project.getName() + "'");
    }
    
    /**
     * Gets the id of this Invitation.
     * @return the id
     */
    public Long getId() {
        return this.id;
    }
    
    public Person getInviter() {
        return inviter;
    }
    
    public void setInviter(Person inviter) {
        this.inviter = inviter;
    }
    
    public Person getInvitee() {
        return invitee;
    }
    
    public void setInvitee(Person invitee) {
        this.invitee = invitee;
    }
    
    /**
     * Get the Project this invitation is for
     */
    public Project getProject() {
        return project;
    }
    
    /**
     * Set the Project this invitation is for.
     */
    public void setProject(Project project) {
        this.project = project;
    }
    
    /**
     *Get the Status of this invitation
     */
    public InvitationStatus getStatus() {
        return status;
    }
    
    /**
     *Set the status of this invitation.
     */
    
    public void setStatus(InvitationStatus status) {
        this.status = status;
        logger.info("'" + invitee.getLogin() + "' sets InvitationStatus for '" + project.getName() + "' to : " + status);
    }
    
    
    /**
     * Returns a hash code value for the object.  This implementation computes
     * a hash code value based on the id fields in this object.
     * @return a hash code value for this object.
     */
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }
    
    /**
     * Determines whether another object is equal to this Invitation.  The result is
     * <code>true</code> if and only if the argument is not null and is a Invitation object that
     * has the same id field values as this object.
     * @param object the reference object with which to compare
     * @return <code>true</code> if this object is the same as the argument;
     * <code>false</code> otherwise.
     */
    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Invitation)) {
            return false;
        }
        Invitation other = (Invitation)object;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false;
        return true;
    }
    
    /**
     * Returns a string representation of the object.  This implementation constructs
     * that representation based on the id fields.
     * @return a string representation of the object.
     */
    @Override
    public String toString() {
        return "timeitj.persistence.Invitation[id=" + id + "]";
    }
}
